﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using ETL.Entity;
using ETL.Common.Logging;
namespace ETL.DataAccess
{
    public class ProjectUserRepository
    {

        public IEnumerable<APP_USERS> GetAllAppUser()
        {
            try
            {
                var db = new ETLDataEntities();
                var result = from s in db.APP_USERS
                                 orderby s.SURNNAME
                                 select s;
                return result;
            }
            catch (Exception ex)
            {
                LogService.Service.Error(ex);
                throw;
            }
        }

        public APP_USERS GetAppUserByUserId(string userId)
        {
            try
            {
                var db = new ETLDataEntities();
                var result = (from s in db.APP_USERS
                              where s.DSE_ID.ToUpper().Equals(userId.ToUpper())
                              select s).FirstOrDefault();
                return result;
            }
            catch (Exception ex)
            {
                LogService.Service.Error(ex);
                return null;
            }
        }

        public IEnumerable<APP_USERS> GetAppUserOfTask(decimal taskId)
        {
            try
            {
                var db = new ETLDataEntities();

                var result = from t in db.TASKS
                             join s in db.SCHEDULED_RESOURCE_USAGES on t.GEN_TASK_ID equals s.GEN_TASK_ID
                             join r in db.RESOURCE_INSTANCES on s.GEN_RESOURCE_ID equals r.GEN_RESOURCE_ID
                             join p in db.PROJECTS on t.GEN_PROJECT_ID equals p.PROJECT_ID
                             join a in db.APP_USERS on r.RESOURCE_NAME.ToUpper() equals a.DSE_ID.ToUpper()
                             where t.TASK_ID == taskId
                             orderby t.GEN_TASK_ID
                             select a;

                return result;

            }
            catch (Exception ex)
            {
                LogService.Service.Error(ex);
                return null;
            }
        }
    }
}
